package com.food.eat.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author：RenPu
 * @Package：com.food.eat.config
 * @Project：food-eat-market
 * @name：ExcuterConfig
 * @Date：2024/4/9 23:03
 * @Filename：ExcuterConfig
 */

@Slf4j
@Configuration
public class ExcuterConfig {

    @Bean("threadPoolExecutorDefine")
    public ExecutorService getThreadPool(){
        return new ThreadPoolExecutor(
                Runtime.getRuntime().availableProcessors()+1,
                Runtime.getRuntime().availableProcessors()*2,60L,
                TimeUnit.SECONDS,new LinkedBlockingQueue<>(1024),
                new ThreadFactoryBuilder()
                .setUncaughtExceptionHandler((thread,throwable)->log.error("food-eat-pool {} get exception",thread,throwable))
                .setNameFormat("food-eat-pool-%d").build());
    }
}
